package com.together.sfipp.service.impl;

import com.together.sfipp.mapper.UserMapper;
import com.together.sfipp.pojo.User;
import com.together.sfipp.service.UserService;
import com.together.sfipp.utils.Md5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public void register(User user) {
        //加密
        String md5String = Md5Util.getMD5String(user.getPassword());
        user.setPassword(md5String);
        //添加
        userMapper.insert(user);
    }


    @Override
    public User findByUserCard(String idCard){
        User u = userMapper.findByUserCard(idCard);
        return u;
    }

    @Override
    public void insert(User user) {
        //加密
        String md5String = Md5Util.getMD5String(user.getPassword());
        user.setPassword(md5String);
        if(user.isStudent() != true){
            user.setStudent(false);
        }
        userMapper.insert(user);
    }

    @Override
    public void delete(Integer userId) {
        userMapper.delete(userId);
    }

    @Override
    public void update(User user) {
        if(user.getPassword() != null){
            String md5String = Md5Util.getMD5String(user.getPassword());
            user.setPassword(md5String);
        }
        userMapper.update(user);
    }

    @Override
    public List<User> selectAll() {
        return userMapper.selectAll();
    }

    @Override
    public List<User> selectAll(User user) {
        return userMapper.selectNeed(user);
    }
}
